#include "bsp.h"
#include "rl_usb.h"
#include "main_interface.h"
#include "uart.h"
#include "DAP.h"
#include "target_board.h"

/*
    // 开启宏定义
    DAPLINK_VERSION=257,            // DAPLink 固件版本号
    DAPLINK_HIC_ID=0x97969908,      // HIC（Hardware Interface Controller，硬件接口控制器）的唯一 ID
    DAPLINK_BUILD_KEY=0x9B939E8F,   // 固件构建密钥，类似 “数字指纹
    STM32F103xB,
    INTERFACE_STM32F103XB,
    OS_CLOCK=72000000,
    OS_TICK=10000                   // 定义系统滴答周期为10000 微秒（10ms）
    OS_TICK_FREQ=100,               // 定义 RTOS（实时操作系统）的滴答频率为 100Hz
    USE_HAL_DRIVER,                 // 启用 STM32 的HAL（硬件抽象层）驱动库

    __packed=__packed,              // 定义结构体紧凑对齐属性，强制编译器对结构体不插入填充字节（按实际字节对齐）
    DAPLINK_NO_ASSERT_FILENAMES,    // 禁用断言（assert）中的文件名信息

    // 已删除
    DRAG_N_DROP_SUPPORT,            // 启用 “拖放编程” 功能，结合MSC_ENDPOINT实现
    DAPLINK_IF,                     // 定义 DAPLink 的主接口标识，用于统一管理调试接口
    USE_LEGACY_CMSIS_RTOS,          // 指定使用旧版 CMSIS-RTOS 接口（而非新版 CMSIS-RTOS2）

    HID_ENDPOINT
*/

#define USB_BUSY_TIME (33)         // USB busy time (~3 sec)
#define USB_CONNECT_DELAY (11)     // Delay before a USB device connect may occur (~1 sec)
#define USB_CONFIGURE_TIMEOUT (22) // Timeout for USB being configured (~2 sec)

#define DECZERO(x) (x ? --x : 0) // Decrement to zero
main_usb_connect_t usb_state;    // Global state of usb

extern void cdc_process_event(void); // .\build\DapLink_STM32F103.axf: Error: L6218E: Undefined symbol config_ram_add_hexdump (referred from hardfault_handler.o).

int main(void)
{
    uint32_t usb_state_count = USB_BUSY_TIME;
    uint32_t usb_no_config_count = USB_CONFIGURE_TIMEOUT;

    bsp_init();

    DAP_Setup();   // Initialize the DAP
    init_family(); // initialize the family

    usbd_init(); // USB

    usbd_connect(0);
    usb_state = USB_CONNECTING;
    usb_state_count = USB_CONNECT_DELAY;

    while (1)
    {
        // cdc_process_event();

        if (timer3_fc.trigger_90ms_flag)
        {
            switch (usb_state) // Update USB connect status
            {
            case USB_DISCONNECTING:
                usb_state = USB_DISCONNECTED;
                usbd_connect(0);
                break;

            case USB_CONNECTING:
                if (DECZERO(usb_state_count) == 0) // Wait before connecting
                {
                    usbd_connect(1);
                    usb_state = USB_CHECK_CONNECTED;
                    usb_no_config_count = USB_CONFIGURE_TIMEOUT; // Reset connect timeout
                }
                break;

            case USB_CHECK_CONNECTED:
                if (usbd_configured())
                {
                    // gpio_set_board_power(true); // Let the HIC enable power to the target now that high power has been negotiated.
                    usb_state = USB_CONNECTED;
                }
                else if (DECZERO(usb_no_config_count) == 0)
                {
                    // USB configuration timed out, which most likely indicates that the HIC is
                    // powered by a USB wall wart or similar power source. Go ahead and enable
                    // board power.
                    usb_state = USB_DISCONNECTED;
                }
                break;

            case USB_CONNECTED:
            case USB_DISCONNECTED:
                if (usbd_configured())
                {
                    usb_state = USB_CONNECTED;
                }
                else
                {
                    usb_state = USB_DISCONNECTED;
                    usb_state_count = USB_CONNECT_DELAY;
                    usb_no_config_count = USB_CONFIGURE_TIMEOUT;
                }
            }

            timer3_fc.trigger_90ms_flag = 0;
        }
    }
}
